//
// Created by fanghr on 17-12-24.
//

#ifndef PROJECT_CAM_SINGLEMAT_H
#define PROJECT_CAM_SINGLEMAT_H

#include <opencv2/core/core.hpp>
#include <boost/serialization/access.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>

namespace boost {
    namespace serialization {
        template<class Archive>
        void serialize(Archive &ar, cv::Mat &mat, const unsigned int) {
            int cols, rows, type;
            bool continuous;

            if (Archive::is_saving::value) {
                cols = mat.cols;
                rows = mat.rows;
                type = mat.type();
                continuous = mat.isContinuous();
            }

            ar & cols & rows & type & continuous;

            if (Archive::is_loading::value)
                mat.create(rows, cols, type);

            if (continuous) {
                const unsigned int data_size = rows * cols * mat.elemSize();
                ar & boost::serialization::make_array(mat.ptr(), data_size);
            } else {
                const unsigned int row_size = cols * mat.elemSize();
                for (int i = 0; i < rows; i++) {
                    ar & boost::serialization::make_array(mat.ptr(i), row_size);
                }
            }

        }
    }
}

class cam_singleMat {
private:
    friend class boost::serialization::access;

    cv::Mat mat;

    template<class Archive>
    void serialize(Archive &ar, const unsigned int) {
        ar & mat;
    }

public:
    cam_singleMat() {}

    cam_singleMat(cv::Mat mat) {
        this->mat = mat;
    }

    cv::Mat get() {
        return this->mat;
    }
};

#endif //PROJECT_CAM_SINGLEMAT_H
